package org.rsbot.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;
import javax.swing.filechooser.FileSystemView;
import org.rsbot.log.LogFormatter;
import org.rsbot.log.SystemConsoleHandler;
import org.rsbot.log.TextAreaLogHandler;

public class GlobalConfiguration
{
  public static final String NAME = "RSBot";
  public static final String NAME_LOWERCASE = "RSBot".toLowerCase();
  public static final String SITE_NAME = "Powerbot";
  private static final OperatingSystem CURRENT_OS;
  public static boolean RUNNING_FROM_JAR = false;

  public static Image getImage(String paramString1, String paramString2)
  {
    try
    {
      return Toolkit.getDefaultToolkit().getImage(RUNNING_FROM_JAR ? GlobalConfiguration.class.getResource(paramString1) : new File(paramString2).toURI().toURL());
    }
    catch (Exception localException) {
    }
    return null;
  }

  public static OperatingSystem getCurrentOperatingSystem() {
    return CURRENT_OS;
  }

  public static String getHttpUserAgent() {
    String str1 = "Windows"; String str2 = "Windows NT 5.2";
    if (getCurrentOperatingSystem() == OperatingSystem.MAC) {
      str1 = "Macintosh";
      str2 = "Intel Mac OS X 10_6_4";
    } else if (getCurrentOperatingSystem() != OperatingSystem.WINDOWS) {
      str1 = "X11";
      str2 = "Linux i686";
    }
    StringBuilder localStringBuilder = new StringBuilder(125);
    localStringBuilder.append("Mozilla/5.0 (").append(str1).append("; U; ").append(str2);
    localStringBuilder.append("; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7");
    return localStringBuilder.toString();
  }

  public static URLConnection getURLConnection(URL paramURL, String paramString) throws IOException {
    URLConnection localURLConnection = paramURL.openConnection();
    localURLConnection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    localURLConnection.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    localURLConnection.addRequestProperty("Accept-Encoding", "gzip,deflate");
    localURLConnection.addRequestProperty("Accept-Language", "en-us,en;q=0.5");
    localURLConnection.addRequestProperty("Connection", "keep-alive");
    localURLConnection.addRequestProperty("Host", paramURL.getHost());
    localURLConnection.addRequestProperty("Keep-Alive", "300");
    if (paramString != null) {
      localURLConnection.addRequestProperty("Referer", paramString);
    }
    localURLConnection.addRequestProperty("User-Agent", getHttpUserAgent());
    return localURLConnection;
  }

  public static int getVersion() {
    try {
      FileInputStream localFileInputStream = RUNNING_FROM_JAR ? GlobalConfiguration.class.getClassLoader().getResourceAsStream("resources/version.dat") : new FileInputStream(Paths.VERSION);

      int i = 0;
      byte[] arrayOfByte = new byte[2];
      while (i += localFileInputStream.read(arrayOfByte, i, 2 - i) != 2);
      return ((0xFF & arrayOfByte[0]) << 8) + (0xFF & arrayOfByte[1]);
    } catch (Exception localException) {
      localException.printStackTrace();
    }
    return -1;
  }

  static
  {
    URL localURL = GlobalConfiguration.class.getClassLoader().getResource("resources/version.dat");
    if (localURL != null) {
      RUNNING_FROM_JAR = true;
    }

    String str1 = System.getProperty("os.name");
    if (str1.contains("Mac"))
      CURRENT_OS = OperatingSystem.MAC;
    else if (str1.contains("Windows"))
      CURRENT_OS = OperatingSystem.WINDOWS;
    else if (str1.contains("Linux"))
      CURRENT_OS = OperatingSystem.LINUX;
    else {
      CURRENT_OS = OperatingSystem.UNKNOWN;
    }

    ArrayList localArrayList = new ArrayList();
    localArrayList.add(Paths.getHomeDirectory());
    localArrayList.add(Paths.getLogsDirectory());
    localArrayList.add(Paths.getCacheDirectory());
    localArrayList.add(Paths.getSettingsDirectory());
    if (RUNNING_FROM_JAR) {
      localArrayList.add(Paths.getScriptsDirectory());
      localArrayList.add(Paths.getScriptsSourcesDirectory());
      localArrayList.add(Paths.getScriptsPrecompiledDirectory());
    }

    for (Object localObject1 = localArrayList.iterator(); ((Iterator)localObject1).hasNext(); ) { str2 = (String)((Iterator)localObject1).next();
      localObject2 = new File(str2);
      if (!((File)localObject2).exists()) {
        ((File)localObject2).mkdirs();
      }
    }

    localObject1 = new Properties();
    String str2 = LogFormatter.class.getCanonicalName();
    Object localObject2 = FileHandler.class.getCanonicalName();
    ((Properties)localObject1).setProperty("handlers", TextAreaLogHandler.class.getCanonicalName() + "," + (String)localObject2);
    ((Properties)localObject1).setProperty(".level", "INFO");
    ((Properties)localObject1).setProperty(SystemConsoleHandler.class.getCanonicalName() + ".formatter", str2);
    ((Properties)localObject1).setProperty((String)localObject2 + ".formatter", str2);
    ((Properties)localObject1).setProperty(TextAreaLogHandler.class.getCanonicalName() + ".formatter", str2);
    ((Properties)localObject1).setProperty((String)localObject2 + ".pattern", Paths.getLogsDirectory() + File.separator + "%u.%g.log");
    ((Properties)localObject1).setProperty((String)localObject2 + ".count", "10");
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try {
      ((Properties)localObject1).store(localByteArrayOutputStream, "");
      LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(localByteArrayOutputStream.toByteArray()));
    }
    catch (Exception localException1) {
    }
    if (RUNNING_FROM_JAR) {
      String str3 = localURL.toString();
      try {
        str3 = URLDecoder.decode(str3, "UTF-8");
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException) {
      }
      if (str3.indexOf("jar:file:/") == 0) {
        str3 = str3.substring("jar:file:/".length());
        str3 = str3.substring(0, str3.indexOf(33));
        if (File.separatorChar != '/')
          str3 = str3.replace('/', File.separatorChar);
        try
        {
          File localFile = new File(Paths.getPathCache());
          if (localFile.exists()) {
            localFile.delete();
          }
          localFile.createNewFile();
          BufferedWriter localBufferedWriter = new BufferedWriter(new FileWriter(Paths.getPathCache()));
          localBufferedWriter.write(str3);
          localBufferedWriter.close();
        } catch (Exception localException2) {
          localException2.printStackTrace();
        }
      }
    }
  }

  public static class Paths
  {
    public static final String ROOT = new StringBuilder().append(".").append(File.separator).append("resources").toString();
    public static final String COMPILE_SCRIPTS_BAT = "Compile-Scripts.bat";
    public static final String COMPILE_SCRIPTS_SH = "compile-scripts.sh";
    public static final String COMPILE_FIND_JDK = "FindJDK.bat";
    public static final String ROOT_IMG = new StringBuilder().append(ROOT).append(File.separator).append("images").toString();
    public static final String ICON = new StringBuilder().append(ROOT_IMG).append(File.separator).append("icon.png").toString();
    public static final String ICON_DELETE = new StringBuilder().append(ROOT_IMG).append(File.separator).append("delete.png").toString();
    public static final String ICON_PLAY = new StringBuilder().append(ROOT_IMG).append(File.separator).append("control_play_blue.png").toString();
    public static final String ICON_PAUSE = new StringBuilder().append(ROOT_IMG).append(File.separator).append("control_pause.png").toString();
    public static final String ICON_ADD = new StringBuilder().append(ROOT_IMG).append(File.separator).append("add.png").toString();
    public static final String ICON_ADD_OVER = new StringBuilder().append(ROOT_IMG).append(File.separator).append("add_over.png").toString();
    public static final String ICON_ADD_DOWN = new StringBuilder().append(ROOT_IMG).append(File.separator).append("add_down.png").toString();
    public static final String ICON_HOME = new StringBuilder().append(ROOT_IMG).append(File.separator).append("home.png").toString();
    public static final String ICON_BOT = new StringBuilder().append(ROOT_IMG).append(File.separator).append("bot.png").toString();
    public static final String ICON_CLOSE = new StringBuilder().append(ROOT_IMG).append(File.separator).append("close.png").toString();
    public static final String ICON_CLOSE_OVER = new StringBuilder().append(ROOT_IMG).append(File.separator).append("close_over.png").toString();
    public static final String ICON_TICK = new StringBuilder().append(ROOT_IMG).append(File.separator).append("tick.png").toString();
    public static final String ICON_MOUSE = new StringBuilder().append(ROOT_IMG).append(File.separator).append("mouse.png").toString();
    public static final String ICON_KEYBOARD = new StringBuilder().append(ROOT_IMG).append(File.separator).append("keyboard.png").toString();
    public static final String ICON_CONNECT = new StringBuilder().append(ROOT_IMG).append(File.separator).append("connect.png").toString();
    public static final String ICON_DISCONNECT = new StringBuilder().append(ROOT_IMG).append(File.separator).append("disconnect.png").toString();
    public static final String ICON_START = new StringBuilder().append(ROOT_IMG).append(File.separator).append("control_play.png").toString();
    public static final String ICON_SCRIPT_BDL = new StringBuilder().append(ROOT_IMG).append(File.separator).append("script_bdl.png").toString();
    public static final String ICON_SCRIPT_DRM = new StringBuilder().append(ROOT_IMG).append(File.separator).append("script_drm.png").toString();
    public static final String ICON_SCRIPT_PRE = new StringBuilder().append(ROOT_IMG).append(File.separator).append("script_pre.png").toString();
    public static final String ICON_SCRIPT_SRC = new StringBuilder().append(ROOT_IMG).append(File.separator).append("script_src.png").toString();

    public static final String VERSION = new StringBuilder().append(ROOT).append(File.separator).append("version.dat").toString();
    public static final String SCRIPTS_NAME_SRC = "scripts";
    public static final String SCRIPTS_NAME_OUT = "Scripts";

    public static String getAccountsFile()
    {
      String str;
      if (GlobalConfiguration.getCurrentOperatingSystem() == GlobalConfiguration.OperatingSystem.WINDOWS)
        str = new StringBuilder().append(System.getenv("APPDATA")).append(File.separator).append("RSBot").append("_Accounts.ini").toString();
      else {
        str = new StringBuilder().append(getUnixHome()).append(File.separator).append(".").append(GlobalConfiguration.NAME_LOWERCASE).append("acct").toString();
      }
      return str;
    }

    public static String getHomeDirectory() {
      String str = System.getenv(new StringBuilder().append("RSBot".toUpperCase()).append("_HOME").toString());
      if ((str == null) || (str.isEmpty())) {
        return new StringBuilder().append(GlobalConfiguration.getCurrentOperatingSystem() == GlobalConfiguration.OperatingSystem.WINDOWS ? FileSystemView.getFileSystemView().getDefaultDirectory().getAbsolutePath() : getUnixHome()).append(File.separator).append("RSBot").toString();
      }
      return str;
    }

    public static String getLogsDirectory() {
      return new StringBuilder().append(getHomeDirectory()).append(File.separator).append("Logs").toString();
    }

    public static String getMenuCache() {
      return new StringBuilder().append(getSettingsDirectory()).append(File.separator).append("Menu.txt").toString();
    }

    public static String getPathCache() {
      return new StringBuilder().append(getSettingsDirectory()).append(File.separator).append("path.txt").toString();
    }

    public static String getBootCache() {
      return new StringBuilder().append(getSettingsDirectory()).append(File.separator).append("boot.txt").toString();
    }

    public static String getUIDsFile() {
      return new StringBuilder().append(getSettingsDirectory()).append(File.separator).append("uid.txt").toString();
    }

    public static String getScreenshotsDirectory() {
      return new StringBuilder().append(getHomeDirectory()).append(File.separator).append("Screenshots").toString();
    }

    public static String getScriptsDirectory() {
      return new StringBuilder().append(getHomeDirectory()).append(File.separator).append("Scripts").toString();
    }

    public static String getScriptsSourcesDirectory() {
      return new StringBuilder().append(getScriptsDirectory()).append(File.separator).append("Sources").toString();
    }

    public static String getScriptsPrecompiledDirectory() {
      return new StringBuilder().append(getScriptsDirectory()).append(File.separator).append("Precompiled").toString();
    }

    public static String getCacheDirectory() {
      return new StringBuilder().append(getHomeDirectory()).append(File.separator).append("Cache").toString();
    }

    public static String getScriptsExtractedCache() {
      return new StringBuilder().append(getCacheDirectory()).append(File.separator).append("script.dat").toString();
    }

    public static String getVersionCache() {
      return new StringBuilder().append(getCacheDirectory()).append(File.separator).append("info.dat").toString();
    }

    public static String getModScriptCache() {
      return new StringBuilder().append(getCacheDirectory()).append(File.separator).append("ms.dat").toString();
    }

    public static String getClientCache() {
      return new StringBuilder().append(getCacheDirectory()).append(File.separator).append("client.dat").toString();
    }

    public static String getSettingsDirectory() {
      return new StringBuilder().append(getHomeDirectory()).append(File.separator).append("Settings").toString();
    }

    public static String getUnixHome() {
      String str = System.getProperty("user.home");
      return str == null ? "~" : str;
    }

    public static class URLs
    {
      public static final String UPDATER = "http://links.powerbot.org/";
      public static final String DOWNLOAD = "http://links.powerbot.org/update";
      public static final String UPDATE = "http://links.powerbot.org/modscript";
      public static final String VERSION = "http://links.powerbot.org/version";
      public static final String PROJECT = "http://code.google.com/p/rsbot-client";
      public static final String SITE = "http://www.powerbot.org";
      public static final String STATS = "http://stats.powerbot.org/sync/";
      public static final String GOLD4RS = "http://links.powerbot.org/gold4rs";
      public static final String GOLD4RS_IMG = "http://links.powerbot.org/gold4rs-img";
    }

    public static class Resources
    {
      public static final String ROOT = "resources";
      public static final String THEME = "theme.pb";
      public static final String SCRIPTS = "scripts/";
      public static final String ROOT_IMG = "/resources/images";
      public static final String ICON = "/resources/images/icon.png";
      public static final String ICON_DELETE = "/resources/images/delete.png";
      public static final String ICON_PLAY = "/resources/images/control_play_blue.png";
      public static final String ICON_PAUSE = "/resources/images/control_pause.png";
      public static final String ICON_ADD = "/resources/images/add.png";
      public static final String ICON_ADD_OVER = "/resources/images/add_over.png";
      public static final String ICON_ADD_DOWN = "/resources/images/add_down.png";
      public static final String ICON_HOME = "/resources/images/home.png";
      public static final String ICON_BOT = "/resources/images/bot.png";
      public static final String ICON_CLOSE = "/resources/images/close.png";
      public static final String ICON_CLOSE_OVER = "/resources/images/close_over.png";
      public static final String ICON_TICK = "/resources/images/tick.png";
      public static final String ICON_MOUSE = "/resources/images/mouse.png";
      public static final String ICON_KEYBOARD = "/resources/images/keyboard.png";
      public static final String ICON_CONNECT = "/resources/images/connect.png";
      public static final String ICON_DISCONNECT = "/resources/images/disconnect.png";
      public static final String ICON_START = "/resources/images/control_play.png";
      public static final String ICON_SCRIPT_BDL = "/resources/images/script_bdl.png";
      public static final String ICON_SCRIPT_DRM = "/resources/images/script_drm.png";
      public static final String ICON_SCRIPT_PRE = "/resources/images/script_pre.png";
      public static final String ICON_SCRIPT_SRC = "/resources/images/script_src.png";
      public static final String VERSION = "resources/version.dat";
    }
  }

  public static enum OperatingSystem
  {
    MAC, WINDOWS, LINUX, UNKNOWN;
  }
}